package model

import (
	"database/sql"
	"pay/pkg/orm"
	"time"
)

type AlipayToken struct {
	ID            uint32    `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	AppId         string    `gorm:"comment:子商户ID;size:32;not null;uniqueIndex:UNIQ_IDX_ALIPAY_TOKEN_APP"`
	UserId        string    `gorm:"comment:用户ID;size:32;not null"`
	Token         string    `gorm:"comment:Token;size:128;not null"`
	ExpireTime    time.Time `gorm:"comment:过期时间;index:IDX_ALIPAY_TOKEN_EXPIRE,priority:1"`
	RefreshToken  string    `gorm:"comment:RefreshToken;size:128;not null"`
	Status        bool      `gorm:"comment:账户状态;index:IDX_ALIPAY_TOKEN_EXPIRE,priority:2"`
	ApplyNo       string    `gorm:"comment:进件申请号;size:36;"`
	AccountStatus uint8     `gorm:"comment:账户进件状态;type:smallint;not null"`
	CreatedAt     time.Time `gorm:"comment:创建时间"`
	UpdatedAt     time.Time `gorm:"comment:更新时间"`
}

type AlipayConfig struct {
	ID        uint32    `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;not null;index:IDX_ALIPAY_CONFIG_SG,priority:1"`
	PStoreId  uint32    `gorm:"comment:总部/店铺ID;type:integer;not null;index:IDX_ALIPAY_CONFIG_SG,priority:2"`
	TokenId   uint32    `gorm:"comment:支付宝tokenID;type:integer;not null;index:IDX_ALIPAY_CONFIG_TOKEN,priority:1"`
	Name      string    `gorm:"comment:别名;size:32;not null"`
	Status    bool      `gorm:"comment:收款账户状态;index:IDX_ALIPAY_CONFIG_SG,priority:3;index:IDX_ALIPAY_CONFIG_TOKEN,priority:2"`
	Operator  uint32    `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type WechatConfig struct {
	ID            uint32    `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	SgId          uint32    `gorm:"comment:品牌ID;type:integer;not null;index:IDX_WECHAT_CONFIG_SG,priority:1"`
	PStoreId      uint32    `gorm:"comment:总部/店铺ID;type:integer;not null;index:IDX_WECHAT_CONFIG_SG,priority:2"`
	Name          string    `gorm:"comment:别名;size:32;not null"`
	MerchantId    string    `gorm:"comment:子商户号;size:32"`
	AccountType   uint8     `gorm:"comment:账户类型,1~小微商户,2~个体工商户,3~企业商户;type:smallint;not null"`
	PrincipalName string    `gorm:"comment:主体名称;size:100"`
	Status        bool      `gorm:"comment:收款账户状态;index:IDX_WECHAT_CONFIG_SG,priority:3;index:IDX_WECHAT_CONFIG_ACTIVATE,priority:2"`
	ApplyNo       string    `gorm:"comment:进件申请号;size:36;"`
	AccountStatus uint8     `gorm:"comment:账户进件状态;type:smallint;not null;index:IDX_WECHAT_CONFIG_ACTIVATE,priority:1"`
	Operator      uint32    `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt     time.Time `gorm:"comment:创建时间"`
	UpdatedAt     time.Time `gorm:"comment:更新时间"`
}

type ByteDanceConfig struct {
	ID        uint32    `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;not null;index:IDX_BYTEDANCE_CONFIG_SG,priority:1"`
	PStoreId  uint32    `gorm:"comment:总部/店铺ID;type:integer;not null;index:IDX_BYTEDANCE_CONFIG_SG,priority:2"`
	Name      string    `gorm:"comment:别名;size:32;not null"`
	AppId     string    `gorm:"comment:小程序ID;size:32"`
	Status    bool      `gorm:"comment:收款账户状态;index:IDX_BYTEDANCE_CONFIG_SG,priority:3"`
	Operator  uint32    `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type GlobalConfig struct {
	SgId       uint32       `gorm:"comment:品牌ID;type:integer;not null;primaryKey"`
	ConfigType uint8        `gorm:"comment:配置类型，0&未配置~统一品牌配置，1~店铺自行配置，2~部分店铺自行配置"`
	StoreIds   orm.IntArray `gorm:"comment:自定义门店序列;type:integer[]"`
	Operator   uint32       `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt  time.Time    `gorm:"comment:创建时间"`
	UpdatedAt  time.Time    `gorm:"comment:更新时间"`
}

type BootInit struct {
	ID        uint32    `gorm:"comment:自增主键;type:serial;primaryKey;not null"`
	Key       string    `gorm:"comment:键名;not null"`
	Val       string    `gorm:"comment:键值:not null"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

// 收款账户配置
type StoreConfig struct {
	SgId         uint32    `gorm:"comment:品牌ID;type:integer;not null;primaryKey;index:IDX_STORE_CONFIG,priority:1"`
	PStoreId     uint32    `gorm:"comment:总部/店铺ID;type:integer;not null;primaryKey"`
	BizType      uint8     `gorm:"comment:业务类型;type:smallint;primaryKey;index:IDX_STORE_CONFIG,priority:2"`
	PayType      uint8     `gorm:"comment:支付方式;type:smallint;primaryKey;index:IDX_STORE_CONFIG,priority:3"`
	AccountId    uint32    `gorm:"comment:收款账户ID;type:integer"`
	IsCustomized bool      `gorm:"comment:是否门店自行配置"`
	Status       bool      `gorm:"comment:配置状态;index:IDX_STORE_CONFIG,priority:4"`
	Operator     uint32    `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt    time.Time `gorm:"comment:创建时间"`
	UpdatedAt    time.Time `gorm:"comment:更新时间"`
}

// 支付单
type Order struct {
	ID               uint64       `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	OrderNumber      string       `gorm:"comment:支付单号;size:32;uniqueIndex:UNIQ_IDX_ORDER_NUMBER"`
	BizType          uint8        `gorm:"comment:订单类型;index:UNIQ_IDX_ORDER_BIZ_NO,priority:1"`
	BizNo            string       `gorm:"comment:上游请求交易号;size:36;index:UNIQ_IDX_ORDER_BIZ_NO,priority:2"`
	SourceType       uint8        `gorm:"comment:支付场景"`
	PayType          uint8        `gorm:"comment:支付方式"`
	PayConfig        orm.JSON     `gorm:"comment:退款必要信息，支付宝&抖音~app_id，微信~merchant_id+插件信息，会员~可空"`
	CardInfo         orm.JSON     `gorm:"comment:会员信息,用于确保会员积分"`
	OrderKey         string       `gorm:"comment:下单人身份标识;size:16;"`
	Remark           orm.JSON     `gorm:"comment:透传大字段"`
	Amount           uint64       `gorm:"comment:支付金额;type:bigint"`
	Refunded         uint64       `gorm:"comment:已退金额;type:bigint"`
	MerchantDiscount uint64       `gorm:"comment:商家支付优惠金额;type:bigint"`
	PlatformDiscount uint64       `gorm:"comment:平台支付优惠金额;type:bigint"`
	UserPayed        uint64       `gorm:"comment:用户实付;type:bigint"`
	Receive          uint64       `gorm:"comment:商家实收;type:bigint"`
	PayTime          sql.NullTime `gorm:"comment:付款完成时间;index:IDX_ORDER_SG_KEY,priority:3"`
	Status           uint8        `gorm:"comment:订单状态;index:IDX_ORDER_SG_KEY,priority:4"`
	TradeNo          string       `gorm:"comment:第三方交易号;size:64"`
	TradeRemark      orm.JSON     `gorm:"comment:第三方交易大字段"`
	SgId             uint32       `gorm:"comment:品牌ID;type:integer;not null;index:IDX_ORDER_SG_KEY,priority:1"`
	PStoreId         uint32       `gorm:"comment:总部/店铺ID;type:integer;not null;index:IDX_ORDER_SG_KEY,priority:2"`
	CreatedAt        time.Time    `gorm:"comment:下单时间;"`
	UpdatedAt        time.Time    `gorm:"comment:更新时间"`
}

// 退款单
type Refund struct {
	ID               uint64       `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	OrderNumber      string       `gorm:"comment:退款单号;size:32;uniqueIndex:UNIQ_IDX_REFUND_NUMBER"`
	ParentId         uint64       `gorm:"comment:支付单ID;type:bigint;index:IDX_REFUND_ORDER_ID"`
	BizType          uint8        `gorm:"comment:订单类型;uniqueIndex:UNIQ_IDX_REFUND_BIZ_NO,priority:1"`
	BizNo            string       `gorm:"comment:上游请求交易号;size:36;uniqueIndex:UNIQ_IDX_REFUND_BIZ_NO,priority:2"`
	OrderKey         string       `gorm:"comment:下单人身份标识;size:16;"`
	Amount           uint64       `gorm:"comment:退款金额;type:bigint"`
	MerchantDiscount uint64       `gorm:"comment:商家退优惠金额;type:bigint"`
	PlatformDiscount uint64       `gorm:"comment:平台退优惠金额;type:bigint"`
	UserPayed        uint64       `gorm:"comment:用户实退;type:bigint"`
	Receive          uint64       `gorm:"comment:商家实退;type:bigint"`
	PayTime          sql.NullTime `gorm:"comment:退款完成时间;index:IDX_REFUND_SG_KEY,priority:3"`
	Status           uint8        `gorm:"comment:订单状态;index:IDX_REFUND_SG_KEY,priority:4"`
	RefundReason     string       `gorm:"comment:退款原因;size:100"`
	FailedReason     string       `gorm:"comment:失败原因;size:100"`
	OutTradeNo       string       `gorm:"comment:第三方退款请求号（默认等于单号，部分场景需要置换来退款）;size:64"`
	TradeNo          string       `gorm:"comment:第三方交易号;size:64"`
	TradeRemark      orm.JSON     `gorm:"comment:第三方交易大字段"`
	SgId             uint32       `gorm:"comment:品牌ID;type:integer;not null;index:IDX_REFUND_SG_KEY,priority:1"`
	PStoreId         uint32       `gorm:"comment:总部/店铺ID;type:integer;not null;index:IDX_REFUND_SG_KEY,priority:2"`
	Operator         uint32       `gorm:"comment:操作员工ID;type:integer"`
	CreatedAt        time.Time    `gorm:"comment:下单时间;"`
	UpdatedAt        time.Time    `gorm:"comment:更新时间"`
}

// 执行成功后自动删除
type Schedule struct {
	ScheduleType uint8     `gorm:"comment:1~关闭支付单,2~支付查询,3~退款查询;primaryKey"`
	OrderId      uint64    `gorm:"comment:对应自增ID;primaryKey"`
	ExecuteTime  time.Time `gorm:"comment:下一次重试时间;index:IDX_PAY_SCHEDULE"`
	Retry        uint32    `gorm:"comment:重试次数;type:integer;not null;default:0"`
	CreatedAt    time.Time `gorm:"comment:创建时间"`
	UpdatedAt    time.Time `gorm:"comment:更新时间"`
}

// 事务性消息
type NsqMessage struct {
	ID          uint64       `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	Topic       string       `gorm:"comment:topic;size:36;not null"`
	MsgKey      string       `gorm:"comment:消息唯一标识;size:36;not null"`
	Payload     orm.JSON     `gorm:"comment:消息内容"`
	Status      uint8        `gorm:"comment:消息状态， 0~初始，1~成功，2~标记成功;type:smallint;not null;default:0"`
	ExecuteTime sql.NullTime `gorm:"comment:下一次执行时间;index:IDX_PAY_NSQ_EXECUTE"`
	Retry       uint32       `gorm:"comment:重试次数;type:integer;not null;default:0"`
	CreatedAt   time.Time    `gorm:"comment:创建时间"`
	UpdatedAt   time.Time    `gorm:"comment:更新时间"`
}
